package jimm;

import DrawControls.*;
import jimm.comm.*;
import jimm.comm.Icq;
import java.util.*;
import jimm.util.ResourceBundle;
import java.io.*;
import javax.microedition.lcdui.*;

import DrawControls.ListItem;
import DrawControls.VirtualList;
import DrawControls.TextList;
import DrawControls.VirtualListCommands;
import DrawControls.PopupList;
import DrawControls.PopupListCommands;

import jimm.comm.SearchAction;
import jimm.comm.Action;
import jimm.comm.UpdateContactListAction;

import jimm.Spy;



class GMpList extends PopupList  implements PopupListCommands
{
	public PopupHolder menuholder;
	public Object parent;
	public Displayable backScreen = null;
	
	static TextList messText;
	int backplace = 0;

	// items collection 
	private ListItem[] items;

	private int lastItemIndex = -1;

	public static final int CMD_BACK = 123456;

	
	public GMpList()
	{
		super(); //null);

		setVLCommands(this);
		JimmUI.setColorScheme(this);

		// #sijapp cond.if target is "MIDP2" | target is "SIEMENS2"#
		if(Options.getBoolean(Options.OPTION_CL_SMALL_FONT))
		setFontSize(VirtualList.SMALL_FONT);
		else
		setFontSize(VirtualList.MEDIUM_FONT);
		// #sijapp cond.else#
		setFontSize((imageList.getHeight() < 16) ? VirtualList.SMALL_FONT : VirtualList.MEDIUM_FONT);
		// #sijapp cond.end#

		this.setCursorMode(4);

		lastItemIndex = -1;
	}

	public ListItem newListItem(int imageIndex, String txt, int value)
	{
		ListItem li = new ListItem();
		li.imageIndex = imageIndex;
		li.text = txt;
		li.color = Options.getSchemeColor(Options.CLRSCHHEME_TEXT);
		li.image = null;
		li.itemValue = value;
		return li;
	}

	public ListItem newListItem(Image image, String txt, int value)
	{
		ListItem li = newListItem(0, txt, value);
		li.image = image;
		return li;	
	}


	public void setImages(ImageList il)
	{
		setImageList(il);           
	}

	public ListItem getItem(int index)
	{
		if (index >= items.length)
			return null;

		return items[index];
	}

	public void setItemsCount(int count, boolean doEmpty)
	{
		if (doEmpty)
		{
			lastItemIndex = -1;
		}

		ListItem[] items1 = new ListItem[count];

		// move all elements from old array to the new
		for (int i = 0; i <= lastItemIndex; i++)
		{
			items1[i] = items[i];
		}
		
		// move pointer to new data array
		items = items1;

		System.gc();
	}

	// add item to array
	public void addItem(int imageIndex, String txt, int value)
	{
		// if new item requres mem. BAD !!!!!
		if (lastItemIndex + 1 >= items.length)
		{
			setItemsCount(lastItemIndex + 2, false);
		}

		lastItemIndex++;

		items[lastItemIndex] = new ListItem();

		items[lastItemIndex].text = txt;
		items[lastItemIndex].imageIndex = imageIndex;
		items[lastItemIndex].color = Options.getSchemeColor(Options.CLRSCHHEME_TEXT);
		items[lastItemIndex].image = null;
		items[lastItemIndex].itemValue = value;

	}
	public void addItem(Image image, String txt, int value)
	{
		addItem(-1, txt, value);
		items[lastItemIndex].image = image;
	}

/*
	public void removeItem(int ind)
	{
		// allocate new array with lower length
		ListItem[] items1 = new ListItem[items.length - 1];

		// copy elements before ind
		for (int i = 0; i < ind; i++)
		{
			items1[i] = items[i];
		}

		// copy elements after ind
		for (int i = ind; i < lastItemIndex; i++)
		{
			items1[i] = items[i + 1];
		}

		items = items1;
//		System.gc();

		if (lastItemIndex == items.length)
			lastItemIndex--;
	}

	public void insertItem(int ind, ListItem item)
	{
		// allocate new array with lower length
		ListItem[] items1 = new ListItem[items.length + 1];

		// copy elements before ind
		for (int i = 0; i < ind; i++)
		{
			items1[i] = items[i];
		}

		// copy elements after ind
		for (int i = ind; i < lastItemIndex; i++)
		{
			items1[i+1] = items[i];
		}
		items1[ind] = item;

		items = items1;
//		System.gc();

		lastItemIndex++;

		if (lastItemIndex == items.length)
			lastItemIndex--;
	}


	public void removeItemLast()
	{
		removeItem(items.length - 1);
	}

	public void setItem(int index, ListItem item)
	{
		if (index > lastItemIndex)
			return;

		items[index] = item;
	}

*/
	
	// VirtualList command impl.
	public void onCursorMove(PopupList sender)
	{
	}
	
	// VirtualList command impl.
	public boolean onKeyPress(PopupList sender, int keyCode, int type)
	{
		int leftKey = Jimm.leftSoftKey;
		int rightKey = Jimm.rightSoftKey;
		int backKey = -11;

		if ((sender == this) && (type == VirtualList.KEY_PRESSED))
		{
			if (keyCode == leftKey)	
			{
				/*if (items[currItem].itemValue == GMpList.CMD_BACK)
				{
					Jimm.display.setCurrent(backScreen);
				}
				else*/
				{
					menuholder.events(parent, items[currItem].itemValue);
				}
				return true;
			}
			else if (keyCode == rightKey || keyCode == backKey) {
				
				//menuholder.events(parent, CMD_BACK);
				Jimm.display.setCurrent(backScreen);
				return true;
			}
		}
		return false;
	//	JimmUI.execHotKey(null, keyCode, type);

		
		
	}
	
	// VirtualList command impl.
	public void onItemSelected(PopupList sender)
	{
		if (sender == this)
		{
			menuholder.events(parent, items[currItem].itemValue);
		}
	}
	
	protected int getSize()
	{
		if (items == null)
		{
			return 0;
		}
		else
		{
			return items.length;
		}
	}
	  
	// returns messages history list item data
	protected void get(int index, ListItem item)
	{
		item.text = items[index].text;
		item.imageIndex = items[index].imageIndex;
		item.image = items[index].image;
		item.color = Options.getSchemeColor(Options.CLRSCHHEME_TEXT);
		item.itemValue = items[index].itemValue;

	}

	public void destroy()
	{
		if (items == null)
			return;

		for (int i = 0; i < items.length; i++)
			items[i] = null;

		parent = null;
		menuholder = null;
		backScreen = null;
	}
}




public class Popup
{
	public GMpList list;

	public void setImageList(ImageList il)
	{
		list.setImages(il);
	}

	public void setItems(ListItem[] li)
	{
	}

	public void setColorScheme()
	{
		if (list != null)
		{
			JimmUI.setColorScheme(list);
			//JimmUI.setColorScheme(list);
		//	if (AllSeeingEyeList.messText != null)
		//		JimmUI.setColorScheme(AllSeeingEyeList.messText);
		}
	}

//	public abstract void show();
	public void show(PopupHolder _this, VirtualList vl)
	{
		list.menuholder = _this;
		list.setBackImage(vl.getBackImage());

		list.backScreen = vl;
		Jimm.display.setCurrent(list);
	}

	public void reset()
	{
		list.setCurrentItem(0);
	}

	public void destroy()
	{
		if (list != null)
			list.destroy();

		list = null;
	}
}

class PopupInChat extends Popup
{
	public static final int CMD_CONTACT_MENU = 1;
	public static final int CMD_DO_COPY		 = 2;
	public static final int CMD_DELETE_CHAT	 = 3;
	public static final int CMD_CLOSE	     = 4; 
	public static final int CMD_DO_PASTE	     = 5;
	public static final int CMD_CANCEL	     = 6;
	public static final int CMD_GOTO_URL	     = 7;
	public static final int CMD_AUTH_REQ	     = 8;

	PopupInChat(int punkts)
	{
		if (list == null)
		{
			list = new GMpList();
			list.parent = (Object)this;
		}

		list.lock();
		
		int itemsCnt = 4;

		boolean ClipNotEmpty = JimmUI.getClipBoardText() != null;
		boolean HasUrl = punkts % 2 == 1;	
		boolean HasAuthReq = punkts >> 1 == 1;

		
		list.setItemsCount(itemsCnt + (HasUrl?1:0) + (HasAuthReq?1:0) + (ClipNotEmpty?1:0), true);
	

		if (HasUrl)
		{
			list.addItem(0, ResourceBundle.getString("goto_url"), CMD_GOTO_URL);
		}

		if (HasAuthReq)
		{
			list.addItem(0, ResourceBundle.getString("grant"), CMD_AUTH_REQ);
		}

		list.addItem(0, ResourceBundle.getString("user_menu"), CMD_CONTACT_MENU);
		list.addItem(0, ResourceBundle.getString("copy_text"), CMD_DO_COPY);

		if (ClipNotEmpty)
		{
			list.addItem(0, ResourceBundle.getString("quote"), CMD_DO_PASTE);
		}

		list.addItem(0, ResourceBundle.getString("delete_chat", ResourceBundle.FLAG_ELLIPSIS), CMD_DELETE_CHAT);
		list.addItem(0, ResourceBundle.getString("close"), CMD_CLOSE);
//		list.addItem(0, ResourceBundle.getString("cancel"), CMD_CANCEL);

		list.unlock();
	}
}

class PopupInEye extends Popup
{
	static public final int CMD_DO_COPY	= 1;
	static public final int CMD_INFO	= 2;
	static public final int CMD_SELECT	= 3;
	static public final int CMD_CLEAR	= 4;
	static public final int CMD_CANCEL	= 5;
//	static public final int CMD_BACK	= 6;


	PopupInEye()
	{
		if (list == null)
		{
			list = new GMpList();
			list.parent = (Object)this;
		}

		list.lock();
		
		list.setItemsCount(5, true);

		list.addItem(0, ResourceBundle.getString("copy_text"), CMD_DO_COPY);
		list.addItem(0, ResourceBundle.getString("history_info"), CMD_INFO);
		list.addItem(0, ResourceBundle.getString("select"), CMD_SELECT);
		list.addItem(0, ResourceBundle.getString("clear"), CMD_CLEAR);
		list.addItem(0, ResourceBundle.getString("close"), GMpList.CMD_BACK);
//		list.addItem(0, ResourceBundle.getString("cancel"), CMD_CANCEL);

		list.unlock();
	}
}

class PopupInStat extends Popup
{
	PopupInStat(String uin)
	{

      	final int CMD_ON		= 1;
	final int CMD_OFF		= 2;
	final int CMD_CLEAR	= 3;
	final int CMD_COPY	= 4;
	final int CMD_COPY_ALL	= 5;
//	final int CMD_BACK	= 6;


		if (list == null)
		{
			list = new GMpList();
			list.parent = (Object)this;
		}

		list.lock();
		
		list.setItemsCount(5, true);

		boolean b = Options.spiedUins.containsKey(uin);

		list.addItem(0, ResourceBundle.getString(b?"stat_off":"stat_on"), b?CMD_OFF:CMD_ON);

		list.addItem(0, ResourceBundle.getString("copy_text"), CMD_COPY);
		list.addItem(0, ResourceBundle.getString("copy_all_text"), CMD_COPY_ALL);

		list.addItem(0, ResourceBundle.getString("clear"), CMD_CLEAR);
		list.addItem(0, ResourceBundle.getString("close"), GMpList.CMD_BACK);


		list.unlock();
	}
}

class PopupInUserInfo extends Popup
{
	static public final int CMD_DO_COPY	= 1;
	static public final int CMD_DO_COPY_ALL	= 2;
	static public final int CMD_DO_EDIT = 4;

	PopupInUserInfo(boolean myInfo)
	{
		if (list == null)
		{
			list = new GMpList();
			list.parent = (Object)this;
		}

		list.lock();
		
		list.setItemsCount(myInfo? 4 : 3, true);

		list.addItem(0, ResourceBundle.getString("copy_text"), CMD_DO_COPY);
		list.addItem(0, ResourceBundle.getString("copy_all_text"), CMD_DO_COPY_ALL);
		if (myInfo)
		{
			list.addItem(0, ResourceBundle.getString("edit"), CMD_DO_EDIT);
		}
		list.addItem(0, ResourceBundle.getString("close"), GMpList.CMD_BACK);

		list.unlock();
	}
}



class PopupInSearch extends Popup
{
	int CMD_PREV	= 1;
	int CMD_NEXT	= 2;
	int CMD_ADD		= 3;
	int CMD_SENDMESSAGE	= 4;
//	int CMD_BACK	= 5;


	PopupInSearch()
	{
		if (list == null)
		{
			list = new GMpList();
			list.parent = (Object)this;
		}

		list.lock();
		
		list.setItemsCount(5, true);

		list.addItem(0, ResourceBundle.getString("next"), CMD_NEXT);
		list.addItem(0, ResourceBundle.getString("prev"), CMD_PREV);
		list.addItem(0, ResourceBundle.getString("add_user"), CMD_ADD);
		list.addItem(0, ResourceBundle.getString("send"), CMD_SENDMESSAGE);
		list.addItem(0, ResourceBundle.getString("close"), GMpList.CMD_BACK);

		list.unlock();
	}

	
}

class PopupInHistory extends Popup
{
	int CMD_SEARCH	= 1;
	int CMD_COPY	= 2;
	int CMD_INFO	= 3;
	int CMD_CLEAR	= 4;


	PopupInHistory()
	{
		if (list == null)
		{
			list = new GMpList();
			list.parent = (Object)this;
		}

		list.lock();
		
		list.setItemsCount(5, true);

	
		list.addItem(0, ResourceBundle.getString("find"), CMD_SEARCH);
		list.addItem(0, ResourceBundle.getString("copy_text"), CMD_COPY);
		list.addItem(0, ResourceBundle.getString("history_info"), CMD_INFO);
		list.addItem(0, ResourceBundle.getString("clear"), CMD_CLEAR);
		list.addItem(0, ResourceBundle.getString("close"), GMpList.CMD_BACK);

		list.unlock();
	}
}


class PopupHolder extends Popup
{
	private PopupInChat pinchat;
	private PopupInEye  pineye;
	public	static PopupInUserInfo pinuserinfo;

	public static PopupInSearch pinsearch;

	public static PopupInHistory pinhistory;

	public static PopupInStat pinspy;
	

	//#sijapp cond.if target is "MIDP2" | target is "SIEMENS2" | target is "MOTOROLA"#
	static public TextList URLList;
	//#sijapp cond.end#


	
	private ChatTextList getCurrentChat(ContactListContactItem item)
	{
		return ChatHistory.getChatHistoryAt(item.getStringValue(ContactListContactItem.CONTACTITEM_UIN));
	}

	public void eventsOfPopupInChat(int val)
	{
		ContactListContactItem cl_item = null;

		if (ContactList.lastChatItem == null)
		{
			cl_item = ContactList.selectedItem();
		}
		else
		{
			cl_item = ContactList.lastChatItem;
		}

		if (val != GMpList.CMD_BACK && cl_item == null)
			Jimm.gm.showMainMenu();//return;

		switch (val)
		{
			
			case PopupInChat.CMD_CONTACT_MENU:
				{
					cl_item.initList(cl_item.getBooleanValue(ContactListContactItem.CONTACTITEM_NO_AUTH), cl_item);
					Jimm.gm.initContactMenu(cl_item, getCurrentChat(cl_item).getDisplayable());
					break;
				}
			case PopupInChat.CMD_DELETE_CHAT:
				{
					String uin = cl_item.getStringValue(ContactListContactItem.CONTACTITEM_UIN);

					ChatHistory.chatHistoryDelete(uin, ChatHistory.DEL_TYPE_CURRENT);
					ContactList.activate();
					break;

				}
			case PopupInChat.CMD_AUTH_REQ:
				{
					cl_item.setIntValue(ContactListContactItem.CONTACTITEM_AUTREQUESTS, cl_item.getIntValue(ContactListContactItem.CONTACTITEM_AUTREQUESTS) - 1);
					SystemNotice notice = new SystemNotice(SystemNotice.SYS_NOTICE_AUTHORISE, cl_item.getStringValue(ContactListContactItem.CONTACTITEM_UIN), true, "");
					SysNoticeAction sysNotAct = new SysNoticeAction(notice);
					try
					{
						Icq.requestAction(sysNotAct);
					}
					catch (JimmException e)
					{
						JimmException.handleException(e);
					}
					Jimm.display.setCurrent(pinchat.list.backScreen);
					break;
					}
			case PopupInChat.CMD_DO_PASTE:
				{
					try
					{
						cl_item.writeMessage(JimmUI.getClipBoardText());
					}
					catch (Exception e)
					{
						Alert alert = new Alert(ResourceBundle
								.getString("text_too_long"));
						Jimm.display.setCurrent(alert, getCurrentChat(cl_item).getDisplayable());
					}
					break;
				}
			case PopupInChat.CMD_DO_COPY:
				{
					String m_uin = cl_item.getStringValue(ContactListContactItem.CONTACTITEM_UIN);
					String m_name = cl_item.getStringValue(ContactListContactItem.CONTACTITEM_NAME);
					ChatHistory.copyText(m_uin, m_name);
					Jimm.display.setCurrent(pinchat.list.backScreen);
					break;
				}
/*
			case GMpList.CMD_BACK:
			case PopupInChat.CMD_CANCEL:
				{
//					Jimm.display.setCurrent(getCurrentChat(cl_item).getDisplayable());
                                      Jimm.display.setCurrent(pinchat.backScreen);
					break;
				}
*/
			case PopupInChat.CMD_CLOSE:
				{
					cl_item.resetUnreadMessages();
					ContactList.activate();
					break;
				}
			case PopupInChat.CMD_GOTO_URL:
			{
			String msg = ChatHistory.getCurrentMessage(cl_item.getStringValue(ContactListContactItem.CONTACTITEM_UIN));
			Vector v = Util.parseMessageForURL(msg);
			if (v.size() == 1)
				try
				{
					Jimm.jimm.platformRequest( (String)v.elementAt(0) );
					Jimm.display.setCurrent(pinchat.list.backScreen);
				}
				catch (Exception e){}
			else
			{       
				URLList = new TextList(ResourceBundle.getString("goto_url"));

				URLList.rightCaption = ResourceBundle.getString("back");
				URLList.leftCaption = ResourceBundle.getString("open");

				URLList.setCursorMode(TextList.SEL_NONE);

				JimmUI.setColorScheme(URLList);

			
				
//				URLList.addCommand(ContactListContactItem.selecturlCommand);
//				URLList.addCommand(ContactListContactItem.backurlCommand);
//				URLList.setCommandListener(cl_item);
				for (int i = 0 ; i < v.size() ; i++)
				{
					URLList.addBigText( (String)v.elementAt(i),cl_item.getTextColor(), Font.STYLE_PLAIN, i).doCRLF(i);
				}
				URLList.leftKeyAction = TextList.LEFT_KEY_GOTOURL;
				URLList.returnable = pinchat.list.backScreen;
				URLList.setFullScreenMode(true);
				

				Jimm.display.setCurrent(URLList);
				//JimmUI.showInfoTextList( URLList ); 

			}
			break;
			}
			
		}
		Jimm.pum.destroyPopupInChat();
	}

	public void eventsOfPopupInEye(int val)
	{
		if (val == GMpList.CMD_BACK)
		{
			Jimm.display.setCurrent(AllSeeingEye.getList());
		}
		else
		{
			AllSeeingEye.processListCommand(val, AllSeeingEye.getList());
		}

		Jimm.pum.destroyPopupInEye();
	}

	public void eventsOfPopupInUserInfo(int val)
	{       
		if (val == GMpList.CMD_BACK)
		{
			Jimm.display.setCurrent(pinuserinfo.list.backScreen);
		}
		else
		{
			JimmUI.processCommandUserInfo(val);
		}
		destroyPopupInUserInfo();
	}

	public void eventsOfPopupInSearch(int val)
	{
		if (val == GMpList.CMD_BACK)
		{
			Jimm.display.setCurrent(pinsearch.list.backScreen);
		}
		else
		{
			Search.searchForm.processPopupCommand(val);
		}
		Jimm.pum.destroyPopupInSearch();
	}
	
	public void eventsOfPopupInHistory(int val)
	{
		if (val == GMpList.CMD_BACK)
		{
			Jimm.display.setCurrent(pinhistory.list.backScreen);
		}
		else
		{
			HistoryStorage.getList().processMenuCommand(val);
		}
		Jimm.pum.destroyPopupInHistory();
	}

	public void eventsOfPopupInStat(int val)
	{   
		if (Spy.getList() != null)
		{
			Spy.getList().processCommand(val, pinspy.list.backScreen);
		}
	}

	public void events(Object list, int val)
	{
		if (list == pinchat)
		{
			eventsOfPopupInChat(val);
		}
		else if (list == pineye)
		{
			eventsOfPopupInEye(val);
		}
		else if (list == pinuserinfo)
		{
			eventsOfPopupInUserInfo(val);
		}
		else if (list == pinsearch)
		{
			eventsOfPopupInSearch(val);
		}
		else if (list == pinhistory)
		{
			eventsOfPopupInHistory(val);
		}
		else if (list == pinspy)
		{
			eventsOfPopupInStat(val);
		}
	}

	PopupHolder()
	{
//		pinuserinfo = new PopupInUserInfo();
//		pinuserinfo.list.menuholder = this;
		                                                   
//		pinsearch = new PopupInSearch();
//		pinsearch.list.menuholder = this;
	}

	public void showPopupInChat(int punkts, VirtualList vl)
	{
		pinchat = new PopupInChat(punkts);
		pinchat.show(this, vl);
	}
	
	public void destroyPopupInChat()
	{
		if (pinchat != null)
		{
			pinchat.destroy();
			pinchat = null;
		}		
		System.gc();
	}

	public void showPopupInEye(VirtualList vl)
	{
		pineye = new PopupInEye();
		pineye.show(this, vl);
	}
	
	public void destroyPopupInEye()
	{
		if (pineye != null)
		{
			pineye.destroy();
			pineye = null;
		}		
		System.gc();
	}

	public Popup createPopupInUserInfo(boolean myInfo)		//showPopupInUserInfo(VirtualList vl)
	{
		//if (pinuserinfo == null)
		{
			pinuserinfo = new PopupInUserInfo(myInfo);
		}

		return pinuserinfo;
//		pinuserinfo.show(this, vl);
	}

	public void destroyPopupInUserInfo()
	{
		if (pinuserinfo != null)
		{
			pinuserinfo.destroy();
			pinuserinfo = null;
		}		
		System.gc();
	}

	public void showPopupInSearch(VirtualList vl)
	{
		pinsearch = new PopupInSearch();
//		return pinsearch;
		pinsearch.show(this, vl);
	}

	public void destroyPopupInSearch()
	{
		if (pinsearch != null)
		{
			pinsearch.destroy();
			pinsearch = null;
		}		
		System.gc();
	}

	public void showPopupInHistory(VirtualList vl)
	{
		pinhistory = new PopupInHistory();
		pinhistory.show(this, vl);
	}

	public void destroyPopupInHistory()
	{
		if (pinhistory != null)
		{
			pinhistory.destroy();
			pinhistory = null;
		}		
		System.gc();
	}

	public void showPopupInSpy(String uin, VirtualList vl)
	{
		pinspy = new PopupInStat(uin);
		pinspy.show(this, vl);
	}

	public void destroyPopupInSpy()
	{
		if (pinspy != null)
		{
			pinspy.destroy();
			pinspy = null;
		}		
		System.gc();
	}



}
